#pragma once
#include <memory>
#include <boost/signals2.hpp>
#include <SFML/Graphics/Color.hpp>
#include <nice2d/sprite.h>
#include <nice2d/component.h>
#include <nice2d/serializer.h>

class Country : public Serializer
{
    friend class CountryManager;
public:
    Country() = delete;
    Country(const sf::Color &color, const std::wstring &name, const std::wstring &desc = std::wstring());
    ~Country();
public:
    std::wstring getName()const;
    sf::Color getColor()const;
    void formAlliance(std::shared_ptr<Country> other, bool alliance = true);

    bool isAllied(const sf::Color &color);
    bool isAllied(std::shared_ptr<Country> other);
    void inactivate();
public:
    bool isEmpty()const;;

    void addSprite(SpritePointer sprite);
    void removeSprite(SpritePointer sprite);
    void tidyup();

    void accept(SpriteVisitor *visitor);
public:
    PTree serialize() override;
    void deserialize(const PTree &mode) override;
private:
    std::unique_ptr<class CountryData> data;
};

using CountryPointer = std::shared_ptr<Country>;

class CountryManager : public Component
{
public:
    CountryManager();
    ~CountryManager();
public:
    CountryPointer createCountry(const sf::Color &color, const std::wstring &name,
                                           const std::wstring &desc = std::wstring());
    CountryPointer getCountry(const sf::Color &color);

    void updateCurrentCountry();
    boost::signals2::signal<void(CountryPointer)> death;

    CountryPointer getCurrentCountry();

    void update(float deltaTime)override;
public:
    PTree serialize()override;
    void deserialize(const PTree &node)override;
private:
    std::unique_ptr<class CountryManagerData> data;
};
